Total Complexity | 9 |
Total Lines | 39 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 0 |
1 | class Sorter { |
||
2 | constructor(key, direction) { |
||
3 | 4 | this.key = key; |
|
4 | 4 | this.moveDirection = direction === 'desc' ? -1 : 1; |
|
5 | } |
||
6 | |||
7 | order(original) { |
||
8 | 4 | return original.sort(this.compare.bind(this)); |
|
9 | } |
||
10 | |||
11 | compare(firstElement, secondElement) { |
||
12 | 22 | let nameA = firstElement[this.key]; |
|
13 | 22 | let nameB = secondElement[this.key]; |
|
14 | |||
15 | 22 | if (typeof nameA === 'string') { |
|
16 | 5 | nameA = nameA.toUpperCase(); |
|
17 | } |
||
18 | |||
19 | 22 | if (typeof nameB === 'string') { |
|
20 | 5 | nameB = nameB.toUpperCase(); |
|
21 | } |
||
22 | |||
23 | 22 | if (nameA < nameB) { |
|
24 | 8 | return -1 * this.moveDirection; |
|
25 | } |
||
26 | |||
27 | 14 | if (nameA > nameB) { |
|
28 | 10 | return 1 * this.moveDirection; |
|
29 | } |
||
30 | |||
31 | 4 | return 0; |
|
32 | } |
||
33 | |||
34 | static create(original, key, direction) { |
||
35 | 4 | const sorter = new Sorter(key, direction); |
|
36 | |||
37 | 4 | return sorter.order(original); |
|
38 | } |
||
39 | } |
||
40 | |||
44 |